<template>
	<view>
		<view v-show="showAd" class="ad-top" @tap="toAdUrl">
			<image class="advert-img" :src="formatUrl(src)" mode="scaleToFill"></image>
			<image class="ad-close" src="/static/icon/close.png" mode="aspectFill" @tap="closeAd"></image>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			showAd: {
				type: Boolean,
				default: true
			},
			src: {
				type: String,
				default: ""
			},
			adUrl: {
				type: String,
				default: ""
			},
			openType: {
				type: String,
				default: 2
			}
		},
		methods: {
			//格式化链接地址
			formatUrl(url) {
				if (url == undefined) {
					return;
				}
				var index = url.indexOf("http");
				if (index == 0) {
					return url;
				}
				return this.$store.state.baseUrl + url;
			},
			toAdUrl() {
				console.log("toUrl",this.openType);
				//有广告，判断类型
				if (this.openType == 1) {
					//调用下载
					this.$store.dispatch('downloadAndInstall', this.adUrl);

				} else if (this.openType == 2) {
					plus.runtime.openURL(this.adUrl);
				}
			},
			closeAd(){
				this.$emit("close");
			}
		},
		data() {
			return {

			};
		}
	}
</script>

<style>
	.ad-top {
		background-color: #FFFFFF; 
		max-height: 300rpx;
		width: 750rpx;
		margin-top: 24rpx;
		position: relative;
	}

	.ad-close {
		position: absolute;
		width: 36rpx;
		height: 36rpx;
		right: 12rpx;
		top: 10rpx;
	}
 
	.advert-img {
		height: 300rpx;
		display:block;
		width: 750rpx;
	}
</style>
